package com.apex.wx.service.impl;
import com.apex.common.util.HttpUtil;
import com.apex.common.util.JsonUtils;
import com.apex.wx.entity.WeixinEntity;
import com.apex.wx.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/4/8
 * Time: 21:01
 */
@Service
public class AccessTokenServiceImpl implements AccessTokenService {

    @Autowired
    private HttpUtil httpUtil;

    @Override
    public String getOpenId(String appId, String code, String secret) {
        StringBuffer url=new StringBuffer("https://api.weixin.qq.com/sns/oauth2/access_token?");
        url.append("appid="+appId);
        url.append("&secret="+secret);
        url.append("&code="+code);
        url.append("&grant_type=authorization_code");
        String msg=httpUtil.responseServiceGet(url.toString());
        WeixinEntity weixinEntity=JsonUtils.toObject(msg,WeixinEntity.class);
        if(null!=weixinEntity){
            return weixinEntity.getOpenid();
        }
        return null;
    }
}
